/*
 * author      : dormi330
 * date        : 2018/7/16
 * description : 退款申请记录
 */

package com.deep.trade.domain.refund;

import com.deep.trade.common.BaseEntity;
import com.deep.trade.common.CommonUtils;
import com.deep.trade.common.TradeException;
import com.deep.trade.domain.order.TradeOrderSku;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class RefundOrder extends BaseEntity {

    public static final int MAX_WAITING_DAYS = 7;

    private String tradeOrderId;            // 订单id
    private String skuListJson;             // sku列表, 不想把sku单独分出去, 单独建表了
    private String refundTypeId;            // 退款类型 id
    private String refundTypeName;          // 退款类型 name
    private Long refundTypeCode;            // 退款类型 code
    private String description;             // 买家的退款说明
    private BigDecimal refundMoney;         // 退款金额
    private String status;                  // 退款申请状态
    private Date actionDeadline;            // 下个步骤 截止日期
    private String rejectReason;            // 拒绝原因
    private Date auditAt;                   // 审核时间

    // 冗余字段
    private String memberId;                // 会员id
    private String sellerId;                // 卖家id
    private String shopId;                  // 店铺Id

    // 非持久化字段
    private List<TradeOrderSku> skuList;        // 非持久化字段
    private RefundOrderStatusEnum statusEnum;   // 状态

    public RefundOrder() {
    }

    // setter getter
    public void setSkuList(List<TradeOrderSku> skuList) {
        this.skuList = skuList;
        if (null != skuList && skuList.size() > 0)
            this.skuListJson = CommonUtils.list2String(skuList);
    }

    public void setStatus(String status) {
        this.status = status;
        this.statusEnum = RefundOrderStatusEnum.fromString(status);
    }

    public void setStatusEnum(RefundOrderStatusEnum statusEnum) {
        this.statusEnum = statusEnum;
        this.status = statusEnum.name;
    }

    public void audit(boolean accept, String rejectReason) throws TradeException {
        if (this.statusEnum.canChangeTo(RefundOrderStatusEnum.SELLER_ACCEPTED)
                || this.statusEnum.canChangeTo(RefundOrderStatusEnum.SELLER_REJECTED)) {
            if (accept) {
                this.setStatusEnum(RefundOrderStatusEnum.SELLER_ACCEPTED);
            } else {
                this.setStatusEnum(RefundOrderStatusEnum.SELLER_REJECTED);
                this.setRejectReason(rejectReason);
            }
            this.setAuditAt(new Date());
        } else {
            throw new TradeException("illegal status");
        }
    }

    // setter getter

    public Date getAuditAt() {
        return auditAt;
    }

    public void setAuditAt(Date auditAt) {
        this.auditAt = auditAt;
    }

    public RefundOrderStatusEnum getStatusEnum() {
        return statusEnum;
    }

    public String getRefundTypeName() {
        return refundTypeName;
    }

    public void setRefundTypeName(String refundTypeName) {
        this.refundTypeName = refundTypeName;
    }

    public Long getRefundTypeCode() {
        return refundTypeCode;
    }

    public void setRefundTypeCode(Long refundTypeCode) {
        this.refundTypeCode = refundTypeCode;
    }

    public String getSkuListJson() {
        return skuListJson;
    }

    public void setSkuListJson(String skuListJson) {
        this.skuListJson = skuListJson;
    }

    public List<TradeOrderSku> getSkuList() {
        return skuList;
    }

    public String getMemberId() {
        return memberId;
    }

    public void setMemberId(String memberId) {
        this.memberId = memberId;
    }

    public String getSellerId() {
        return sellerId;
    }

    public void setSellerId(String sellerId) {
        this.sellerId = sellerId;
    }

    public String getShopId() {
        return shopId;
    }

    public void setShopId(String shopId) {
        this.shopId = shopId;
    }

    public String getTradeOrderId() {
        return tradeOrderId;
    }

    public void setTradeOrderId(String tradeOrderId) {
        this.tradeOrderId = tradeOrderId;
    }

    public String getRefundTypeId() {
        return refundTypeId;
    }

    public void setRefundTypeId(String refundTypeId) {
        this.refundTypeId = refundTypeId;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public BigDecimal getRefundMoney() {
        return refundMoney;
    }

    public void setRefundMoney(BigDecimal refundMoney) {
        this.refundMoney = refundMoney;
    }

    public String getStatus() {
        return status;
    }

    public Date getActionDeadline() {
        return actionDeadline;
    }

    public void setActionDeadline(Date actionDeadline) {
        this.actionDeadline = actionDeadline;
    }

    public String getRejectReason() {
        return rejectReason;
    }

    public void setRejectReason(String rejectReason) {
        this.rejectReason = rejectReason;
    }


}
